/*
 *  $Id: ButtonGroup.cs 719 2009-07-18 17:06:33Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008-2009 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System.Collections.Generic;

using AgateLib.DisplayLib;
using AgateLib.Geometry;

namespace Ragima.Menu {
    public class ButtonGroup {
        private UI.Sprite _sprite;
        private List<Button> _buttons;
        private int _offset;

        public ButtonGroup(UI.Sprite sprite) {
            _sprite = sprite;
            _buttons = new List<Button>();
            _offset = 0;
        }

        public int Offset {
            get { return _offset; }
            set {
                if (value == _offset) return;
                _offset = value;
                UpdatePositions();
            }
        }

        public void AddButton(ButtonNumber number, SimpleEvent action) {
            Surface s = _sprite.CarveSubSurface((int) number);
            s.RotationCenter = OriginAlignment.Center;
            _buttons.Add(new Button(s, action));
            UpdatePositions();
        }

        private void UpdatePositions() {
            if (_buttons.Count == 0) return;
            Size size = _sprite.Descriptor.Size;
            int h = size.Height * _buttons.Count;
            int d = size.Height / 2;
            h += d*(_buttons.Count-1);
            int x, y = (Display.CurrentWindow.Height - h)/2;
            foreach (Button button in _buttons) {
                x = (Display.CurrentWindow.Width - size.Width)/2;
                button.Rect = new Rectangle(x + _offset, y + _offset, size.Width, size.Height);
                y += size.Height + d;
            }
        }

        public void UpdateRegions(List<Region> regions) {
            foreach (Button button in _buttons) regions.Add(button);
        }
    }
}
